//
//  LJMultiSelCell.m
//  TableviewMutilSelect
//
//  Created by raymond on 16/9/8.
//  Copyright © 2016年 raymond. All rights reserved.
//

#import "LJMultiSelCell.h"

@interface LJMultiSelCell ()

@property (nonatomic, assign) BOOL mSelected;
@property (nonatomic, strong) UIImageView *mSelectedIndicator;

@end

@implementation LJMultiSelCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self)
    {
        _mSelected = NO;
        _mSelectedIndicator = [[UIImageView alloc] init];
        [self.contentView addSubview:_mSelectedIndicator];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    CGRect indicatorFrame = CGRectMake(-30, fabs(self.contentView.frame.size.height - 30)/ 2 + 2, 30, 30);
    self.mSelectedIndicator.frame = indicatorFrame;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationBeginsFromCurrentState:YES];

    if (_mSelected)
    {
        self.textLabel.textColor = [UIColor darkTextColor];
        [_mSelectedIndicator setImage:[UIImage imageNamed:@"Selected"]];
    }
    else
    {
        self.backgroundView.backgroundColor = [UIColor clearColor];
        self.textLabel.textColor = [UIColor grayColor];
        [_mSelectedIndicator setImage:[UIImage imageNamed:@"Unselected"]];
    }

    [UIView commitAnimations];
}

- (void)changeMSelectedState
{
    if (!self.isEditing) return;
    _mSelected = !_mSelected;
    [self setNeedsLayout];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    [self changeMSelectedState];
}

@end
